<template>
  <page-header-wrapper>
    <a-card
      :bordered="true"
      :hoverable="true">
      <!-- 条件搜索 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="班级" prop="classId">
                <a-tree-select
                  :value="value"
                  v-model="queryParam.classId"
                  :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
                  :replaceFields="replaceFields"
                  :tree-data="classOptions"
                  placeholder="请选择班级"
                  style="width: 100%"
                  tree-default-expand-all
                >
                </a-tree-select>
              </a-form-item>
            </a-col>

            <a-col :md="8" :sm="24">
              <a-form-item label="学号" prop="userName">
                <a-input v-model="queryParam.userName" allow-clear placeholder="请输入学号" />
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="8" :sm="24">
                <a-form-item label="姓名" prop="nickName">
                  <a-input v-model="queryParam.nickName" allow-clear placeholder="请输入姓名" />
                </a-form-item>
              </a-col>

              <a-col :md="8" :sm="24">
                <a-form-item label="手机" prop="phonenumber">
                  <a-input v-model="queryParam.phonenumber" allow-clear placeholder="请输入手机号码" />
                </a-form-item>
              </a-col>

              <a-col :md="8" :sm="24">
                <a-form-item label="性别" prop="sex">
                  <a-select v-model="queryParam.sex" allow-clear placeholder="请选择性别" style="width: 100%">
                    <a-select-option v-for="(d, index) in sexOptions" :key="index" :value="d.dictValue">
                      {{ d.dictLabel }}
                    </a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="!advanced && 8 || 24" :sm="24">
              <span
                :style="advanced && { float: 'right', overflow: 'hidden' } || {} "
                class="table-page-search-submitButtons">
                <a-button type="primary" @click="handleQuery"><a-icon type="search" />查询</a-button>
                <a-button style="margin-left: 8px" @click="resetQuery"><a-icon type="redo" />重置</a-button>
                <a style="margin-left: 8px" @click="toggleAdvanced">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'" />
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 操作 -->
      <div class="table-operations">
        <a-button v-hasPermi="['platform:student:add']" type="primary" @click="$refs.createForm.handleAdd()">
          <a-icon type="plus" />
          新增
        </a-button>
        <a-button
          v-hasPermi="['platform:student:edit']"
          :disabled="single"
          type="primary"
          @click="$refs.createForm.handleUpdate(undefined, ids)">
          <a-icon type="edit" />
          修改
        </a-button>
        <a-button v-hasPermi="['platform:student:remove']" :disabled="multiple" type="danger" @click="handleDelete">
          <a-icon type="delete" />
          删除
        </a-button>
        <a-button
          type="default"
          @click="$refs.importExcel.importExcelHandleOpen()"
          v-hasPermi="['platform:student:export']">
          <a-icon type="upload" />
          导入
        </a-button>
        <a-button v-hasPermi="['platform:student:export']" type="default" @click="handleExport">
          <a-icon type="download" />
          导出
        </a-button>
        <a-button
          :loading="loading"
          :style="{float: 'right'}"
          icon="reload"
          shape="circle"
          type="dashed"
          @click="getList" />
      </div>
      <!-- 增加修改 -->
      <create-form
        ref="createForm"
        :classOptions="classOptions"
        :sexOptions="sexOptions"
        :specialtyOptions="specialtyOptions"
        @ok="getList"
      />
      <!-- 上传文件 -->
      <import-excel
        ref="importExcel"
        @ok="getList"
      />
      <!-- 详情查看 -->
      <view-form
        ref="viewForm"
        :sexOptions="sexOptions"
      />
      <a-table
        :columns="columns"
        :data-source="list"
        :loading="loading"
        :pagination="false"
        :bordered="true"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        :scroll="{ x: true }"
        :size="tableSize"
        rowKey="studentId">
        <span slot="nickName" slot-scope="text, record">
          <a @click="$refs.viewForm.handleView(record, undefined)" v-if="checkPermi(['platform:student:query'])">{{ record.nickName }}</a>
          <span v-else>{{ record.nickName }}</span>
        </span>
        <span slot="sex" slot-scope="text, record">
          {{ record.sex === '0' ? '男' : (record.sex === '1' ? '女' : '未知') }}
        </span>
        <span slot="operation" slot-scope="text, record">
          <a v-hasPermi="['platform:student:edit']" @click="$refs.createForm.handleUpdate(record, undefined)">
            修改
          </a>
          <a-divider v-hasPermi="['platform:student:remove']" type="vertical" />
          <a style="color:red" v-hasPermi="['platform:student:remove']" @click="handleDelete(record)">
            删除
          </a>
        </span>
      </a-table>
      <!-- 分页 -->
      <a-pagination
        :current="queryParam.pageNum"
        :page-size="queryParam.pageSize"
        :showTotal="total => `共 ${total} 条`"
        :total="total"
        class="ant-table-pagination"
        show-quick-jumper
        show-size-changer
        @change="changeSize"
        @showSizeChange="onShowSizeChange"
      />
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { listStudent, delStudent } from '@/api/platform/student'
import { treeselect } from '@/api/system/dept'
import { optionsPost } from '@/api/system/post'
import { checkPermi } from '@/utils/permission'
import CreateForm from './modules/CreateForm'
import ImportExcel from './modules/ImportExcel'
import ViewForm from './modules/ViewForm'

export default {
  name: 'Student',
  components: {
    CreateForm,
    ImportExcel,
    ViewForm
  },
  data () {
    return {
      replaceFields: { children: 'children', title: 'label', key: 'id', value: 'id' },
      list: [],
      // 班级选项
      classOptions: [],
      // 专业选项
      specialtyOptions: [],
      // 性别列表
      sexOptions: [],
      value: undefined,
      selectedRowKeys: [],
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      ids: [],
      loading: false,
      total: 0,
      // 查询参数
      queryParam: {
        classId: null,
        specialtyId: null,
        userName: null,
        nickName: null,
        phonenumber: null,
        sex: undefined,
        pageNum: 1,
        pageSize: 10
      },
      columns: [
        {
          title: '主键',
          dataIndex: 'studentId',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '姓名',
          dataIndex: 'nickName',
          scopedSlots: { customRender: 'nickName' },
          ellipsis: true,
          align: 'center'
        },
        {
          title: '学号',
          dataIndex: 'userName',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '手机号码',
          dataIndex: 'phonenumber',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '用户性别',
          dataIndex: 'sex',
          scopedSlots: { customRender: 'sex' },
          ellipsis: true,
          align: 'center'
        },
        {
          title: '班级',
          dataIndex: 'className',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '专业',
          dataIndex: 'specialtyName',
          ellipsis: true,
          align: 'center'
        },
        // {
        //   title: '归属用户',
        //   dataIndex: 'ownerUserObject.name',
        //   ellipsis: true,
        //   align: 'center'
        // },
        // {
        //   title: '归属部门',
        //   dataIndex: 'ownerDeptObject.name',
        //   ellipsis: true,
        //   align: 'center'
        // },
        {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operation' },
          align: 'center'
        }
      ]
    }
  },
  filters: {},
  created () {
    // 加载部门列表
    this.getDeptList()
    // 加载专业列表
    this.getSpecialtyList()
    // 加载性别列表
    this.getDicts('sys_user_sex').then(response => {
      this.sexOptions = response.data
    })
    this.resetQuery()
  },
  computed: {},
  watch: {},
  methods: {
    /** 权限判断 */
    checkPermi,
    /** 查询学生信息列表 */
    getList () {
      this.loading = true
      listStudent(this.queryParam).then(response => {
        this.list = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParam.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.queryParam = {
        classId: undefined,
        specialtyId: undefined,
        userName: undefined,
        nickName: undefined,
        phonenumber: undefined,
        sex: undefined,
        pageNum: 1,
        pageSize: 10
      }
      this.handleQuery()
    },
    onShowSizeChange (current, pageSize) {
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    changeSize (current, pageSize) {
      this.queryParam.pageNum = current
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.ids = this.selectedRows.map(item => item.studentId)
      this.single = selectedRowKeys.length !== 1
      this.multiple = !selectedRowKeys.length
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      var that = this
      const studentIds = row.studentId || this.ids
      this.$confirm({
        title: '删除提示',
        content: row.nickName ? '确定要删除' + row.nickName + '的数据吗？' : '确定要删除选中的数据吗？',
        onOk () {
          return delStudent(studentIds)
            .then(() => {
              that.onSelectChange([], [])
              that.getList()
              that.$message.success(
                '删除成功',
                3
              )
            })
        },
        onCancel () {
        }
      })
    },
    /** 导出按钮操作 */
    handleExport () {
      var that = this
      this.$confirm({
        title: '是否确认导出?',
        content: '此操作将导出当前条件下所有数据而非选中数据',
        onOk () {
          that.download('platform/student/export', {
            ...that.queryParam
          }, `student_${new Date().getTime()}.xlsx`)
        },
        onCancel () {
        }
      })
    },
    /** 班级列表 */
    getDeptList () {
      treeselect().then(response => {
        this.classOptions = response.data
      })
    },
    /** 专业列表 */
    getSpecialtyList () {
      optionsPost().then(response => {
        this.specialtyOptions = response.data
      })
    }
  }
}
</script>
